/*
    created: 2023/4/13
    filename: bracematch.c
    author: 李东霖
    purpose: 使用栈完成括号匹配  
*/
#include"../liststack/liststack.h"
//括号匹配，传入一个字符串。 返回值 -1-字符串为空 0-不匹配 1-匹配
int brac_match(const char* str)
{
    if(str == NULL) return -1;
    PListStack stack = InitStack2();
    if(stack == NULL) return -1;
    const char* s = str;
    char elem;
    while(*s != '\0')
    {
        switch (*s)
        {
        case '(':
                elem = '(';
                Push(stack,&elem);
            break;
        case '[':
                elem = '[';
                Push(stack,&elem);           
            break;
        case '{':
                elem = '{';
                Push(stack,&elem);
            break;
        case ')':
                if(1==StackEmpty(stack)) return 0;
                Pop(stack,&elem);
                if(elem != '(')  return 0;
            break;
        case ']':
                if(1==StackEmpty(stack)) return 0;
                Pop(stack,&elem);
                if(elem != '[')  return 0;           
            break;
        case '}':
                if(1==StackEmpty(stack)) return 0;
                Pop(stack,&elem);
                if(elem != '{')  return 0;
            break;
        default:
            break;

        }
        ++s;
    }
    if(StackEmpty(stack)) return 1;
    return 0;
}
int main()
{
    char strp[110] = "){[((1)1)]}";
    if(1 == brac_match(strp))
    {
        printf("匹配\n");
    }
    else
    {
        printf("不匹配\n");
    }
    return 0;
}
